import { Controller } from "../../mvc/core/Controller";
import { IGuildWarehouseExController } from "./interfaces/IGuildWarehouseExController";
import { IView } from "../../mvc/interfaces/IView";
import { GuildWarehouseExModule } from "../../../modules/GuildWarehouseExModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SGuildWarehouseUpdate } from "../../../Message/Game/SGuildWarehouseUpdate";
import { IGuildWarehouseExModule } from "../../../modules/interfaces/IGuildWarehouseExModule";
import { GuildUtil } from "../view/guild/info/GuildUtil";
//class GuildWarehouseExController
    
    export  class GuildWarehouseExController extends Controller implements IGuildWarehouseExController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._guildWarehouseEx == null) 
            {
                this._guildWarehouseEx = new GuildWarehouseExModule();
            }
            return this._guildWarehouseEx;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildWarehouseExUpdate, this.onGuildWarehouseExUpdateHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicGuildWarehouseHasDrop, this.onGuildWarehouseHasDropHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseExOpen, this.onGuildWarehouseExOpenHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseExMoveItem, this.onGuildWarehouseExMoveItemHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseExMoveItemSuccess, this.onGuildWarehouseExMoveItemSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseExGetItems, this.onGuildWarehouseExGetItemsHandler);
            Dispatcher.addEventListener(EventName.GuildWarehouseExGetItemsSuccess, this.onGuildWarehouseExGetItemsSuccessHandler);
            Dispatcher.addEventListener(EventName.GuildElite_LeaveGuildEliteCopy, this.onLeaveGuildEliteCopyHandler);
            return;
        }

        /* internal  */onGuildWarehouseExOpenHandler(arg1: DataEvent): void
        {
            if (view.isHide) 
            {
                view.show();
                GameProxy.guildProxy.getGuildWarehousesEx();
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onGuildWarehouseExUpdateHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SGuildWarehouseUpdate;
            cache.guild.warehouseExUpdate = loc1.items;
            if (_view && !_view.isHide) 
            {
                (view as IGuildWarehouseExModule).updateDatas(loc1.items);
            }
            return;
        }

        /* internal  */onGuildWarehouseHasDropHandler(arg1: MessageBlock): void
        {
            GameProxy.guildProxy.getGuildWarehousesEx();
            return;
        }

        /* internal  */onGuildWarehouseExMoveItemHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.uid as string;
            var loc3=loc1.allMove as Boolean;
            if (cache.guild.warehouseExUpdate.length > 0) 
            {
                GameProxy.guildProxy.moveGuildWarehouseEx(loc2, loc3);
            }
            return;
        }

        /* internal  */onGuildWarehouseExMoveItemSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (this._guildWarehouseEx && !this._guildWarehouseEx.isHide && loc1) 
            {
                this._guildWarehouseEx.flyIconsToGuild();
            }
            return;
        }

        /* internal  */onGuildWarehouseExGetItemsHandler(arg1: DataEvent): void
        {
            GameProxy.guildProxy.getGuildWarehousesEx();
            return;
        }

        /* internal  */onGuildWarehouseExGetItemsSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as Array<any>;
            cache.guild.warehouseExUpdate = loc1;
            if (loc1 && loc1.length > 0) 
            {
                view.show();
            }
            return;
        }

        /* internal  */onLeaveGuildEliteCopyHandler(arg1: DataEvent): void
        {
            if (GuildUtil.isLeader(cache.guild.myGuildInfo)) 
            {
                GameProxy.guildProxy.getGuildWarehousesEx();
            }
            return;
        }

        private /* var */_guildWarehouseEx: IGuildWarehouseExModule;
    }
